package com.jingdianjichi.subject.common.entity;

import lombok.Getter;

import java.util.List;

@Getter
public class PageResult <T>{

    private Integer pageNo;

    private Integer pageSize;

    /*
     * 总条数
     */
    private Integer total;

    /**
     * 总页数
     */
    private Integer totalPages;

    private List<T> result;

    private Integer start;

    private Integer end;

    public PageResult(Integer pageNo, Integer pageSize) {
        this.setPageNo(pageNo); // 自动校验页码
        this.setPageSize(pageSize); // 自动校验每页条数
    }

    public PageResult() {

    }

    public void setRecords(List<T> result){
        if(result!=null && result.size()>0){
            setTotal(result.size());
        }
        this.result = result;
    }

    public void setTotal(Integer total){
        this.total = total;
        if(pageSize > 0){
            this.totalPages = (total/pageSize) + (total % pageSize == 0 ? 0 : 1);
        }else{
            this.totalPages = 1;
        }

        this.start = (this.pageSize > 0 ? (this.pageNo - 1)*pageSize : 0) + 1;
        this.end = (this.start - 1 + pageSize*(pageNo > 0 ? 1 : 0)) ;
    }

    public void setPageNo(Integer pageNo){
        this.pageNo = pageNo < 1 ? 1 : pageNo;
    }

    public void setPageSize(Integer pageSize){
        this.pageSize = pageSize < 1 ? 20 : pageSize;
    }
}
